package com.qinghe.pro.service.api.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.qinghe.pro.entity.api.ApiColumn;
import com.qinghe.pro.exception.ExistedException;
import com.qinghe.pro.mapper.api.ApiColumnMapper;
import com.qinghe.pro.mapper.api.ApiMapper;
import com.qinghe.pro.service.api.ApiColumnService;
import com.qinghe.utils.Utils;

@Service("apiColumnServiceImpl")
public class ApiColumnServiceImpl implements ApiColumnService {

    @Autowired
    private ApiColumnMapper apiColumnMapper;

    @Autowired
    private ApiMapper apiMapper;

	@Override
	public ApiColumn get(Integer id) {
		return apiColumnMapper.selectById(id);
	}

	@Override
	public void insert(ApiColumn apiColumn) {
		apiColumnMapper.insertSelective(apiColumn);
	}

	@Override
	public void update(ApiColumn apiColumn) {
		apiColumnMapper.updateByPrimaryKeySelective(apiColumn);
	}

	@Override
	public void delete(Integer id) {
		if (apiMapper.selectByColumnId(id).size() > 0) {
			throw new ExistedException("该分类下有接口数据，不能删除。");
		}
		apiColumnMapper.deleteByPrimaryKey(id);
	}

	@Override
	public String findMainAsTree() {
		List<ApiColumn> columnList = apiColumnMapper.selectAll();

		return makeTree(columnList, false);
	}

	@Override
	public String findAllAsTree() {
		List<ApiColumn> columnList = apiColumnMapper.selectAll();

		return makeTree(columnList, true);
	}

	/**
	 * 生成下拉树的json字符串
	 * 
	 * @param list
	 * @param hasRoot true:有根节点；false：无根节点
	 * 
	 * @return json
	 * @author longfei
	 */
	private String makeTree(List<ApiColumn> list, boolean hasRoot) {
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
		for(ApiColumn apiColumn : list) {
			if (!hasRoot && apiColumn.getParent() == null) {
				continue;
			}
			Map<String, Object> item = new HashMap<String, Object>();
			item.put("id", apiColumn.getId());
			item.put("name", apiColumn.getName());
			if (apiColumn.getParent() == null) {
				item.put("pId", 0);
			} else {
				item.put("pId", apiColumn.getParent().getId());
			}
			
			result.add(item);
		}
		
		return Utils.JsonUtil.toJson(result);
	}

	@Override
	public ApiColumn getTree() {
		List<ApiColumn> list = apiColumnMapper.selectAllAndCount();

		List<ApiColumn> rootList = makeTree(list);

		return rootList.get(0);
	}

	private List<ApiColumn> makeTree(List<ApiColumn> list) {
		List<ApiColumn> parent = new ArrayList<ApiColumn>();
		// get parentId = null;
		for (ApiColumn e : list) {
			if (e.getParent() == null) {
				e.setChildren(new ArrayList<ApiColumn>(0));
				parent.add(e);
			}
		}
		// 删除parentId = null;
		list.removeAll(parent);

		makeChildren(parent, list);

		return parent;
	}

	private void makeChildren(List<ApiColumn> parent, List<ApiColumn> children) {
		if (children.isEmpty()) {
			return;
		}

		List<ApiColumn> tmp = new ArrayList<ApiColumn>();
		for (ApiColumn c1 : parent) {
			for (ApiColumn c2 : children) {
				c2.setChildren(new ArrayList<ApiColumn>(0));
				if (c1.getId().equals(c2.getParent().getId())) {
					c1.getChildren().add(c2);
					tmp.add(c2);
				}
			}
		}

		children.removeAll(tmp);

		makeChildren(tmp, children);
	}

}
